package com.yy.xshell.loopPull;

import com.yy.xshell.core.ShellContext;
import com.yy.xshell.core.SshCore;
import com.yy.xshell.auth.UserSessionInfo;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.Data;

import java.io.InputStream;

/**
 * @author wxy
 * @version 1.0
 * @date 2024/8/21 18:57
 */
@Data
public class SessionStream {

    private InputStream inputStream;

    private Channel channel;

    private String sessionKey;

    private String userId;

    private UserSessionInfo sessionInfo;

    private ShellContext.Environment environment;

    public void sendMsg() throws Exception{
        byte[] b = new byte[40960];
        while (inputStream.available() > 0){
            int read = inputStream.read(b);
            ShellContext shellContext = new ShellContext(new String(b, 0, read), sessionKey, environment);
            channel.writeAndFlush(new TextWebSocketFrame(SshCore.RESULT_HANDLER_PIPELINE.handler(shellContext).toJsonString()));
        }
    }

    @Override
    public boolean equals(Object obj) {
        if(!(obj instanceof  SessionStream)){
            return false;
        }
        SessionStream stream = (SessionStream) obj;
        return this.userId.equals(stream.getUserId()) && this.sessionKey.equals(stream.getSessionKey());
    }

    @Override
    public String toString() {
        return this.getUserId() + "|" + this.getSessionKey();
    }
}
